package com.jwater.core.manager.worker.assign;

import com.jwater.core.common.S;
import jwater.org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: jnner
 * @createDate: 2021/3/6
 * @version: 1.0
 */
public final class WorkerEnv {
    private Map<String, String> envs = new HashMap<>();

    private volatile static WorkerEnv instance;

    private WorkerEnv() {
    }

    public static WorkerEnv getInstance() {
        if (instance == null) {
            synchronized (WorkerEnv.class) {
                if (instance == null) {
                    instance = new WorkerEnv();
                }
            }
        }
        return instance;
    }

    public static WorkerEnv wrap(String envsString) {
        WorkerEnv instance = new WorkerEnv();
        if (!StringUtils.isEmpty(envsString)) {
            String[] aa = envsString.split(",");
            for (String a : aa) {
                String[] bb = a.split("=");
                instance.envs.put(bb[0], bb[1]);
            }
        }
        return instance;
    }

    public void put(String key, String value) {
        envs.put(key, value);
    }

    public Map<String, String> envs() {
        return envs;
    }

    public String envsToString() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> en : envs.entrySet()) {
            if (sb.length() != 0) {
                sb.append(",");
            }
            sb.append(en.getKey()).append("=").append(en.getValue());
        }
        return sb.toString();
    }
}
